<!DOCTYPE html>
<html lang="en">
<!-- 古韵超市 · 商品详情页面 -->
<?php
//判断是否非法访问
session_start();
if (!isset($_SESSION['USERINFOS'])) {
    echo "<script>alert('非法访问！');window.location.href='login_reg.html';</script>";
    exit();
}

if (empty($_GET['g_id'])) {
    echo "<script>alert('非法传参！');window.location.href='u_all_goods.php';</script>";
    exit();
}

$g_id = $_GET['g_id'];
include_once("php/conn.php");
$sql_goods = "SELECT * FROM tb_goods_info WHERE g_id = '" . $g_id . "'";
$result_goods = mysqli_query($conn, $sql_goods);
if ($result_goods) {
    $row_goods = mysqli_fetch_assoc($result_goods);
    if ($row_goods['g_is_sell'] == '0') {
        echo "<script>alert('该商品已下架！');window.location.href='u_all_goods.php';</script>";
        exit();
    }
} else {
    die("<script>alert('查询商品信息失败！');window.location.href='u_all_goods.php';</script>");
    exit();
}
?>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>古韵超市 · 商品详情</title>
    <link rel="stylesheet" href="bootstrap-5.3.3-dist/bootstrap.min.css">
    <script src="bootstrap-5.3.3-dist/bootstrap.min.js"></script>
    <link rel="stylesheet" href="css/u_public.css">
    <link rel="stylesheet" href="css/show_goods.css">
    <script src="js/show-goods.js"></script>
</head>

<body>
    <!-- 导航 -->
    <div class="navbar fixed-top">
        <nav class="navbar navbar-expand-lg navbar-light bg-light" id="navbar">
            <div class="container-fluid">
                <div class="logo_div">
                    <img src="img/LOGO.png">
                </div>
                <a class="navbar-brand" href="#">古韵超市网上购物系统</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse"
                    data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
                    aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                            <a class="nav-link" href="../index.php">首页</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="u_all_goods.php">所有商品</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="u_mycart.php">我的购物车</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="u_show_order.php">我的订单</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="u_history_order.php">历史订单</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="u_personal_info.php">我的信息</a>
                        </li>
                    </ul>
                    <div class="infobox">
                        当前登录：<?php echo $_SESSION['USERINFOS']['uname']; ?> |
                        <a href="php/logout.php">退出登录</a>
                    </div>
                </div>
            </div>
        </nav>
    </div>
    <main>
        <div class="back_div">
            <button type="button" class="btn btn-outline-primary" onclick="window.history.go(-1)">返回</button>
        </div>
        <div class="info_title_div">
            商品详情页
        </div>
        <div class="goods_info_div">
            <div class="goods_info_img_div">
                <img src="../goods_imgs/<?php echo $row_goods['g_img']; ?>">
            </div>
            <div class="goods_info_item_div">
                <div class="goods_title">
                    <h3><?php echo $row_goods['g_name']; ?></h3>
                </div>
                <form action="php/send-order.php" method="post" class="goods_info_form">
                    <input type="hidden" name="goods_id" id="goods_id" value="<?php echo $row_goods['g_id']; ?>">
                    <input type="hidden" name="uid" id="uid" value="<?php echo $_SESSION['USERINFOS']['uid']; ?>">
                    <div class="mb-3 row">
                        <label for="address" class="col-sm-2 col-form-label">配送地址</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="address" name="address" required>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label for="name" class="col-sm-2 col-form-label">收获人</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="name" name="name" value="<?php echo $_SESSION['USERINFOS']['uname']; ?>" required>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label for="phone" class="col-sm-2 col-form-label">联系方式</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" id="phone" name="phone" value="<?php echo $_SESSION['USERINFOS']['uphone']; ?>" required>
                        </div>
                    </div>

                    <div class="mb-3 row">
                        <label for="type" class="col-sm-2 col-form-label">套餐类型</label>
                        <div class="col-sm-10">
                            <input type="radio" class="btn-check" name="g_specs" id="g_specs" value="<?php echo $row_goods['g_specs']; ?>" checked>
                            <label class="btn btn-outline-secondary" for="g_specs"><?php echo $row_goods['g_specs']."Kg"; ?></label>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label for="goods_num" class="col-sm-2 col-form-label">库存</label>
                        <div class="col-sm-10">
                            <input type="text" readonly class="form-control-plaintext" id="goods_num"
                                value="<?php echo $row_goods['g_num'] - $row_goods['g_sell_num']; ?> ">
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label for="num" class="col-sm-2 col-form-label">单价</label>
                        <div class="col-sm-2">
                            <input type="text" class="form-control" id="price" name="price" value="<?php echo $row_goods['g_price']; ?> 元" readonly>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label for="num" class="col-sm-2 col-form-label">购买数量</label>
                        <div class="col-sm-2">
                            <input type="number" class="form-control" id="num" name="num" value="1" onchange="count_price()">
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label for="ensure" class="col-sm-2 col-form-label">保障</label>
                        <div class="col-sm-10">
                            <input type="text" readonly class="form-control-plaintext" id="ensure"
                                value="保证新鲜  准时装  损坏包换  不满包退">
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label for="ensure" class="col-sm-2 col-form-label">描述</label>
                        <div class="col-sm-10">
                            <input type="text" readonly class="form-control-plaintext" id="g_describe"
                                value="<?php echo $row_goods['g_describe']; ?>">
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <label for="pay" class="col-sm-2 col-form-label">订单金额</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" id="pay" name="pay" value="<?php echo $row_goods['g_price']; ?> 元" readonly>
                        </div>
                    </div>
                    <div class="mb-3 row">
                        <div class="col-sm-6">
                            <button type="submit" class="btn btn-danger btn-style" onclick="return submit_order();">立即购买</button>
                        </div>
                        <div class="col-sm-6">
                            <button type="button" class="btn btn-warning btn-style" onclick="return add_cart();">加入购物车</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="remark_div">
            <form class="remark_form" action="php/send-remark.php" method="post">
                <div class="mb-3">
                    <input type="hidden" name="way" value="1">
                    <input type="hidden" name="g_id" value="<?php echo $row_goods['g_id']; ?>">
                    <input type="hidden" name="uid" value="<?php echo  $_SESSION['USERINFOS']['uid']; ?>">
                    <label for="remark" class="form-label">发表评论</label>
                    <textarea class="form-control textarea" name="remark" id="remark" rows="4"></textarea>
                </div>
                <div class="remark_btn">
                    <button type="sbumit" class="btn btn-primary btn-style r_btn" onclick="return add_remark();">发表评论</button>
                </div>
            </form>
        </div>
        <div class="remark_list">
            <div class="remark_title">
                <h3>用户评论</h3>
            </div>
            <div class="remark_content">
                <?php
                include_once("php/get-remark.php");
                getRemark($g_id);
                ?>
            </div>
        </div>
    </main>
    <footer class="fixed-bottom">
        古韵超市网上购物系统© 2025® All Rights Reserved.
    </footer>
</body>

</html>